package cn.uexpo.lhz.controller.admin;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.poi.excel.BigExcelWriter;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import cn.hutool.poi.excel.StyleSet;
import cn.uexpo.lhz.biz.StaffBiz;
import cn.uexpo.lhz.common.MqttGateway;
import cn.uexpo.lhz.common.oldBase.BaseController;
import cn.uexpo.lhz.common.oldUtils.PageRequest;
import cn.uexpo.lhz.common.oldUtils.PageResult;
import cn.uexpo.lhz.plus.R;
import cn.uexpo.lhz.common.utils.JsonData;
import cn.uexpo.lhz.core.http.HttpResult;
import cn.uexpo.lhz.domain.Staff;
import cn.uexpo.lhz.domain.StaffExample;
import cn.uexpo.lhz.domain.vo.StaffVo;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 人员管理木块
 * 这是我有史以来见过最垃圾的代码
 * 能不能用点心
 *
 * @param
 * @return
 */
@RestController
@RequestMapping("/cert/staff")
public class StaffController extends BaseController<Staff> {
    @Autowired
    StaffBiz staffBiz;

    @Autowired
    StaffBiz staffService;

    @Autowired
    private MqttGateway mqttGateway;//等于网关的作用


    /**
     * 查询所有人员并且分页
     * 列表
     *
     * @param staff
     * @return
     */
    @RequestMapping("/findPage")
    public HttpResult findPage(@RequestBody Staff staff) {
        PageRequest pageRequest = new PageRequest();
        PageResult result = null;
        if (!"".equals(staff.getPage()) && !"".equals(staff.getLimit())) {
            pageRequest.setPage(staff.getPage());
            pageRequest.setLimit(staff.getLimit());
        }
        result = staffBiz.findStaffPage(pageRequest, staff);
        //指定科目和搜索关键词都是不为空的情况下
        if (!"".equals(staff.getSelected_program()) && null != staff.getSelected_program() && !"".equals(staff.getSelected_search()) && null != staff.getSelected_search()) {
            StaffExample example = new StaffExample();
            StaffExample.Criteria criteria = example.createCriteria();
            //获取相对应的指定科目数据时,搜索关键词搜索对应的字段值
            if ("姓名".equals(staff.getSelected_program())) {
                criteria.andStaffNameEqualTo(staff.getSelected_search());
            } else if ("单位名称".equals(staff.getSelected_program())) {
                criteria.andStaffUnitEqualTo(staff.getSelected_search());
            } else if ("手机号".equals(staff.getSelected_program())) {
                criteria.andStaffTelEqualTo(staff.getSelected_search());
            } else if ("身份证件类别".equals(staff.getSelected_program())) {
                criteria.andStaffCardEqualTo(staff.getSelected_search());
            } else if ("身份证件号码".equals(staff.getSelected_program())) {
                criteria.andStaffCardidEqualTo(staff.getSelected_search());
            } else if ("标签".equals(staff.getSelected_program())) {
                criteria.andProjectLabelEqualTo(staff.getSelected_search());
            } else if ("项目角色".equals(staff.getSelected_program())) {
                criteria.andProjectRoleEqualTo(staff.getSelected_search());
            }
            example.setOrderByClause("into_time desc");
            result = staffBiz.findPage(pageRequest, example);
        }
        return HttpResult.ok(result);
    }


    /**
     * 动态查看人员数接口
     * 条件:staffId  staffNumber
     *
     * @param staff
     * @return
     */
    @RequestMapping("/selectStaffIdToStaffNumber")
    public Object selectStaffIdToStaffNumber(Staff staff) {
        List<Staff> staffList = staffBiz.selectStaffIdToStaffNumber(staff);
        JSONObject jObject = new JSONObject();
        if (staffList.size() > 0 && staffList != null) {
            jObject.put("code", 200);
            jObject.put("msg", "成功");
            jObject.put("data", staffList);
        } else {
            jObject.put("code", 500);
            jObject.put("msg", "失败");
        }
        return jObject;
    }

    /**
     * 根据项目名，身份证ID，人员ID
     * 返回进入次数给Pad
     *
     * @param staff
     * @return
     */
    @RequestMapping("/selectStaffuntitled6")
    public Object selectStaffuntitled6(Staff staff) {
        Staff s = staffBiz.selectStaffuntitled6(staff);
        JSONObject jObject = new JSONObject();
        if (s != null) {
            jObject.put("code", 200);
            jObject.put("untitled6", s.getUntitled6());
        } else {
            jObject.put("code", 500);
            jObject.put("msg", "失败");
        }
        return jObject;
    }

    /**
     * 根据人员ID批量操作人员状态
     *
     * @param list
     * @return
     */
    @RequestMapping("/updateByStaffStates")
    public Object updateByStaffStates(@RequestBody List<Integer> list) {
        int result = staffBiz.updateByStaffStates(list);
        if (result > 0) {
            for (int i = 0; i < list.size(); i++) {
                JSONObject jObject = new JSONObject();
                jObject.put("staffId", list.get(i));
                jObject.put("staffState", "1");
                String data = jObject.toString();
                //把传递数据放入mq当中(主题方式)
                mqttGateway.sendToMqtt(data, "staffState");//发送staffMsg主题
            }
        }
        JsonData jsonData = new JsonData();
        jsonData.setCode(200);
        jsonData.setData(result);
        return jsonData;
    }

    /**
     * 根据人员ID进行批量删除
     *
     * @param staffId
     * @return
     */
    @PostMapping("/deleteByStaffIds")
    public R deleteByStaffIds(@RequestBody List<Integer> staffId) {
        int result = staffBiz.deleteByStaffIds(staffId);

        return R.ok().put("result", result);
    }


    /**
     * 根据项目名称删除所有相关人员数据
     *
     * @param projectType
     * @return
     */
    @GetMapping("/deleteAllByprojectType")
    public R deleteAllByProjectType(@RequestParam String projectType) {
        return R.ok().put("index", staffService.deleteAllByprojectType(projectType));
    }


    /**
     * 只查询人员ID
     *
     * @param staff
     * @return 提供给pad
     */
    @RequestMapping("/selectStaffId")
    @ResponseBody
    public Object selectStaffId(Staff staff) {
        List<Staff> result = staffBiz.selectStaffId(staff);
        String data = null;
        if (result != null) {
            JSONObject jObject = new JSONObject();
            jObject.put("data", result);
            data = jObject.toString();
        }
        return data;
    }

    /**
     * 根据项目名只查询人员ID
     *
     * @param staff
     * @return 提供给pad
     */
    @RequestMapping("/selectStaffIdWhereProjectName")
    @ResponseBody
    public Object selectStaffIdWhereProjectName(Staff staff) {
        List<Staff> result = staffBiz.selectStaffIdWhereProjectName(staff);
        String data = null;
        if (result != null) {
            JSONObject jObject = new JSONObject();
            jObject.put("data", result);
            data = jObject.toString();
        }
        //System.out.println(data);
        return data;
    }

    /**
     * 根据人员ID去获取人员信息
     *
     * @param staff
     * @return 提供给pad
     */
    @RequestMapping("/selectStaffIdToStaffSingle")
    @ResponseBody
    public Object selectStaffIdToStaffSingle(Staff staff) {
        List<Staff> staffList = staffBiz.selectStaffIdToStaffSingle(staff);
        JsonData jsonData = new JsonData();
        jsonData.setCode(200);
        jsonData.setData(staffList);
        return jsonData;
    }

    /**
     * 根据ID删除单个人员
     *
     * @param
     * @return
     */
    @GetMapping("/deleteStaffSingle")
    public R deleteStaffSingle(@RequestParam String staffId) {
//        System.out.println(Integer.parseInt(staffId));
        staffBiz.deleteByPrimaryKey(Integer.parseInt(staffId));
        return R.ok().put("data", "已删除该人员信息");
    }

    /**
     * 根据人员ID存取人员照片特征值
     * 根据人员ID更新人员照片特征值状态
     *
     * @param staff
     * @return
     */
    @RequestMapping("/updateStaffImageTrait")
    @ResponseBody
    public Object updateStaffImageTrait(Staff staff) {
        int result = staffBiz.updateByPrimaryKeySelective(staff);
        JSONObject jObject = new JSONObject();
        if (result > 0) {
            jObject.put("code", 200);
            jObject.put("msg", "成功");
            jObject.put("untitled5", staff.getUntitled5());
        } else {
            jObject.put("code", 500);
            jObject.put("msg", "失败");
        }
        return jObject;
    }

    /**
     * 根据人员ID查找图片信息
     *
     * @param staff
     * @return
     */
    //提供给设备的接口
    @RequestMapping("/findStaffImage")
    @ResponseBody
    public Object findStaffImage(Staff staff) {
        List<Staff> list = staffBiz.selectPartStaff(staff.getStaffId());
        Map<String, Object> json = new HashMap<>();
        json.put("data", list);
        return json;
    }

    //提供给前端的接口
    @GetMapping("/findStaffImage2")
    public R findStaffImage2(@RequestParam String staffId) {
        List<Staff> list = staffBiz.selectPartStaff(Integer.parseInt(staffId));
        return R.ok().put("list", list);
    }

    /**
     * 根据人员ID查找单个人员信息
     *
     * @param staff
     * @return
     */
    @RequestMapping("/findStaffSingle")
    public Object findStaffSingle(@RequestBody Staff staff) {
        staffBiz.selectByPrimaryKey(staff.getStaffId());
        return JsonData.toJsonObject("查询成功", "200", true, staff);
    }

//    /**
//     * 增加单个人员信息
//     * @param staff
//     * @return
//     * @throws Exception
//     */
//
//    @PreAuthorize("hasAuthority('biz:importStaff:new')")
//    @RequestMapping("/addStaffSingle")
//    @ResponseBody
//    public Object addStaffSingle(@RequestBody Staff staff)throws Exception{
//        /**
//         * 设置默认值
//         */
//        staff.setStaffState("0");//设置人员状态默认为0 正常
//        staff.setUntitled6("0");//bilibili  进入场次次数状态
//        /**
//         * 截取存储前端已转Base64字码的图片
//         */
//        String test = String.valueOf(staff.getOriginalImage());
//        staff.setStaffOriginalImage(Base64.decodeBase64(test));
//
//        String test2=String.valueOf(staff.getHeadImage());
//        staff.setStaffHeadImage(Base64.decodeBase64(test2));
//        /**
//         * 时间类型的转换处理 人员录入时间
//         */
//        Date date=new Date();     //获取一个Date对象
//        SimpleDateFormat simpleDateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   //创建一个格式化日期对象
//        String time = simpleDateFormat.format(date);   //格式化后的时间
//        Date times=simpleDateFormat.parse(time); //String-->Date
//        staff.setIntoTime(times);
//
//
//        int result=staffBiz.insert(staff);
//
//        if(result>0){
//            Staff s=new Staff();
//            s.setStaffId(staff.getStaffId());
//            List<Staff> staffList=staffBiz.selectStaffIdToStaffSingle(s);
//            JSONArray jArray1 = new JSONArray();
//            JSONArray jArray2 = new JSONArray();
//            Map<String,Object> mapforstaff = new HashMap<>();
//            Map<String,Object> mapforstaffchildren = new HashMap<>();
//            jArray2.add(mapforstaffchildren.put("staffChildren", staffList));
//            jArray1.add(mapforstaff.put("data",jArray2));
//            mqttGateway.sendToMqtt(jArray1.toString(),"staffMsg");
//        }
//        return JsonData.toJsonObject("新增人员成功", "200", true, staff);
//    }

    /**
     * 增加单个人员信息
     *
     * @param staff
     * @return
     * @throws Exception
     */

    @RequestMapping("/addStaffSingle")
    public Object addStaffSingle(@RequestBody Staff staff) throws Exception {
        staff.setStaffState("0");//设置人员状态默认为0 正常
        staff.setUntitled6("0");//bilibili  进入场次次数状态
        /**
         * 截取存储前端已转Base64字码的图片
         */
        String test = String.valueOf(staff.getOriginalImage());
        staff.setStaffOriginalImage(Base64.decodeBase64(test));

        String test2 = String.valueOf(staff.getHeadImage());
        staff.setStaffHeadImage(Base64.decodeBase64(test2));
        /**
         * 时间类型的转换处理 人员录入时间
         */
        Date date = new Date();     //获取一个Date对象
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   //创建一个格式化日期对象
        String time = simpleDateFormat.format(date);   //格式化后的时间
        Date times = simpleDateFormat.parse(time); //String-->Date
        staff.setIntoTime(times);


        int result = staffBiz.insert(staff);
        if (result > 0) {
            JSONArray jArray1 = new JSONArray();
            JSONArray jArray2 = new JSONArray();
            JSONArray jArray3 = new JSONArray();

            Map<String, Object> mapforstaff = new HashMap<>();
            Map<String, Object> mapforstaffchildren = new HashMap<>();

            mapforstaff.put("data", jArray2);
            mapforstaffchildren.put("staffChildren", jArray3);

            jArray1.add(mapforstaff);
            jArray2.add(mapforstaffchildren);

            staff.setOriginalImage("");
            staff.setHeadImage("");
            staff.setStaffOriginalImage(null);
            staff.setStaffHeadImage(null);

            jArray3.add(staff);

            String staffMsg = jArray1.toString();
            mqttGateway.sendToMqtt(staffMsg, "staffMsg");
        }
        return JsonData.toJsonObject("新增人员成功", "200", true, staff);
    }


    /**
     * 家博会人员接口
     *
     * @param param
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/homechinaexpocert", method = RequestMethod.POST)
    @ResponseBody
    public Object homechinaexpocert(@RequestBody String param) throws Exception {
        String AccountID = request.getHeader("AccountID");
        String PIN = request.getHeader("PIN");
        if (AccountID.equals("uexpo202006homechinaexpo") && PIN.equals("2020expo6jbhfoo4w")) {
            // 对象转换:将String类型转成json
            JSONObject jsonObject = JSONObject.parseObject(param);
            if (jsonObject == null) {
                JsonData jsonData = new JsonData();
                jsonData.setCode(407);
                return jsonData;
            }
            //将数组转成jsonArray
            JSONArray jsonArray = jsonObject.getJSONArray("data");
            //jsonArray 迭代器
            Iterator<Object> jsonIterator = jsonArray.iterator();
            //System.out.println(jsonObject.toJSONString());
            if (jsonArray == null) {
                JsonData jsonData = new JsonData();
                jsonData.setCode(406);
                return jsonData;
            }
            //map转成json数据
            JSONArray jArray1 = new JSONArray();
            ;
            JSONArray jArray2 = new JSONArray();
            ;
            Map<String, Object> mapforstaff = new HashMap<>();
            mapforstaff.put("data", jArray2);
            String data = null;
            int zero = 0;
            //循环获取
            while (jsonIterator.hasNext()) {
                Staff staff = new Staff();
                //获取数据并转化成JSONObject
                JSONObject parseObject = (JSONObject) jsonIterator.next();
                staff.setStaffName(parseObject.getString("nameofattendee"));
                staff.setStaffCardid(parseObject.getString("idofattendee"));
                staff.setProjectType(parseObject.getString("project"));
                staff.setStaffState("0");
                staff.setStaffCard("身份证");
                staff.setIntoAccount("2020华夏家博会");
                /**
                 * 时间类型的转换处理
                 */
                Date date = new Date();     //获取一个Date对象
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   //创建一个格式化日期对象
                String time = simpleDateFormat.format(date);   //格式化后的时间
                Date times = simpleDateFormat.parse(time); //String-->Date
                staff.setIntoTime(times);

                StaffExample example = new StaffExample();
                StaffExample.Criteria criteria = example.createCriteria();
                criteria.andStaffNameEqualTo(staff.getStaffName());
                criteria.andStaffCardidEqualTo(staff.getStaffCardid());
                criteria.andProjectTypeEqualTo(staff.getProjectType());
                List<Staff> staffList = staffBiz.selectByExample(example);

                if (staffList.size() > 0) {
                    continue;
                } else {
                    staffBiz.insert(staff);
                    Map<String, Object> mapforstaffchildren = new HashMap<>();
                    jArray2.add(mapforstaffchildren);
                    JSONArray jArray3 = new JSONArray();
                    ;
                    mapforstaffchildren.put("staffChildren", jArray3);
                    jArray3.add(staff);
                    zero = 1;
                }
            }
            //把传递数据放入mq当中(主题方式)
            if (zero == 0) {
                JsonData jsonData = new JsonData();
                jsonData.setCode(200);
                return jsonData;
            }
            jArray1.add(mapforstaff);
            data = jArray1.toString();
            mqttGateway.sendToMqtt(data, "staffMsg");
            JsonData jsonData = new JsonData();
            jsonData.setCode(200);
            return jsonData;
        } else {
            JsonData jsonData = new JsonData();
            jsonData.setCode(403);
            return jsonData;
        }
    }

    /**
     * 根据ID选择性更新人员信息
     *
     * @param staff
     * @return
     */
    @RequestMapping("/updateStaffSingle")
    public Object updateStaffSingle(@RequestBody Staff staff) {
        /**
         * 截取存储前端已转Base64字码的图片
         */
        String test = String.valueOf(staff.getOriginalImage());
        staff.setStaffOriginalImage(Base64.decodeBase64(test));

        String test2 = String.valueOf(staff.getHeadImage());
        staff.setStaffHeadImage(Base64.decodeBase64(test2));

        staffBiz.updateByPrimaryKeySelective(staff);
        //修改人员的原图照片时,需要告诉pad  待解决
/*		Map<String,Object> map = new HashMap<>();
		map.put("msg", 200);
		byte[] StaffOriginalImage=staff.getStaffOriginalImage();
		map.put("data", StaffOriginalImage);
		//map转成json数据
		JSONArray jArray = new JSONArray();
		jArray.add(map);
		String data=jArray.toString();
		//把传递数据放入mq当中(主题方式)
		mqttGateway.sendToMqtt(data,"staffUpdateImageMsg");//发送staffMsg主题*/
        return JsonData.toJsonObject("人员信息已更新", "200", true, staff);
    }

    /**
     * 更新人员状态
     *
     * @param staff
     * @return
     */
    @PostMapping("/updateStaffState")
    public R updateStaffState(@RequestBody Staff staff) {
        int result = staffBiz.updateByStaffState(staff);
        if (result > 0) {
            JSONObject jObject = new JSONObject();
            jObject.put("staffId", staff.getStaffId());
            jObject.put("staffState", staff.getStaffState());
            String data = jObject.toString();
            //把传递数据放入mq当中(主题方式)
            mqttGateway.sendToMqtt(data, "staffState");//发送staffMsg主题
        }
        return R.ok().put("staff", staff);
//        return JsonData.toJsonObject("人员状态已改变", "200", true, staff);
    }


    /**
     * 血的教训 -------------------》》只怪你太菜、
     * Vue的传递参数没有认真检查,params,this.params的问题
     * 后端这主要是因为@RequestBody注解
     * 实体一转实体类二(用于DO,PO,BO,VO之间的互转):这个方法也可行  此代码放在这里作为后面可以遇到相似问题的第二种解决方式
     */
    //    @RequestMapping(value="/addStaffSingle",method = RequestMethod.POST)
    //    @ResponseBody
    //    public Object addStaffSingle(@RequestBody StaffVo staffv)throws Exception{
    //    	Staff staff = (Staff)BeanVBConvertor.Bean2Bean(staffv, Staff.class);
    //        staff.setStaffState("0");//设置人员状态默认为0 正常
    //        /**
    //         * 截取存储前端已转Base64字码的图片
    //         */
    //        String test = String.valueOf(staff.getOriginalImage());
    //        //System.out.println(test);
    //        //System.out.println(test.substring(22));
    //        System.out.println(test);
    //        staff.setStaffOriginalImage(Base64.decodeBase64(test));
    //        /**
    //         * 时间类型的转换处理
    //         */
    //        Date date=new Date();     //获取一个Date对象
    //        SimpleDateFormat simpleDateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   //创建一个格式化日期对象
    //        String time = simpleDateFormat.format(date);   //格式化后的时间
    //        Date times=simpleDateFormat.parse(time); //String-->Date
    //        staff.setIntoTime(times);
    //
    //        staffBiz.insert(staff);
    //        return JsonData.toJsonMessage("人员信息新增成功！","200",true);
    //    }

    /**
     * 传递参数的第二种方式int id=Integer.parseInt(request.getParameter("id"));
     * String 类型
     */
    /*    @RequestMapping("/sendStaffMsg")
    @ResponseBody
    public String sendStaffMsg() {
    	int id=Integer.parseInt(request.getParameter("id"));
    	StaffExample example=new StaffExample();

    	StaffExample.Criteria criteria= example.createCriteria();//根据条件查询
//    	if(staff.getStaffId()!=null) {
    		criteria.andStaffIdEqualTo(id);
//    	}
    	Staff record=staffBiz.selectByPrimaryKey(id);
//    	example.setOrderByClause("into_time desc");

    	List<Staff> list=staffBiz.selectByExample(example);
    	String jsonStr=JsonData.toJsonObject("查询成功","200",true,record).toString();
    	mqttGateway.sendToMqtt(jsonStr,"staffMsg");//发送staffMsg主题
    	return "sendStaffMsgOk";
    }*/

//    @PostMapping("/findPageExport")
//    public void findPageExport(@RequestBody Staff staff, HttpServletResponse response) throws IOException {
//        PageRequest pageRequest = new PageRequest();
//        PageResult result = null;
//        if (!"".equals(staff.getPageNum()) && !"".equals(staff.getPageSize())) {
//            pageRequest.setPageNum(staff.getPageNum());
//            pageRequest.setPageSize(staff.getPageSize());
//        }
//            result = staffBiz.findStaffPage(pageRequest, staff);
//            List<Staff> content = (List<Staff>) result.getContent();
//            long startTime = System.currentTimeMillis();   //获取开始时间
//            List<StaffVo> collect = content.stream().map((e) -> {
//                StaffVo staffVo = new StaffVo();
//                BeanUtils.copyProperties(e, staffVo);
//                return staffVo;
//            }).collect(Collectors.toList());
//
//            List<StaffVo> rows = null;
//            for (int i = 0; i < collect.size(); i++) {
//                rows = CollUtil.newArrayList(collect);
//            }
//
//            // 通过工具类创建writer，默认创建xls格式
//            ExcelWriter writer = ExcelUtil.getWriter();
//            getStyle(writer);
//
//            // 一次性写出内容，使用默认样式，强制输出标题
//            writer.write(rows, true);
//            //out为OutputStream，需要写出到的目标流
//            long endTime = System.currentTimeMillis(); //获取结束时间
//            long l = endTime - startTime;
//            System.out.println("导出共耗时>>>>>>>>" + l + "毫秒");
//            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
//            response.setHeader("Content-Disposition", "attachment;filename=test.xlsx");
//            ServletOutputStream out = response.getOutputStream();
//            writer.flush(out, true);
//            writer.close();
//            IoUtil.close(out);
//        }
    private void getStyle(ExcelWriter writer) {
        // 定义单元格背景色
        StyleSet style = writer.getStyleSet();
        CellStyle headCellStyle = style.getHeadCellStyle();
        // 第二个参数表示是否也设置头部单元格背景
        // style.setBackgroundColor(IndexedColors.RED, false);
        //. 自定义字体
        Font font = writer.createFont();
        font.setBold(true);
        font.setColor(Font.COLOR_RED);
        font.setItalic(true);
        //第二个参数表示是否忽略头部样式
        writer.getStyleSet().setFont(font, true);

        //自定义标题别名
        writer.addHeaderAlias("staffName", "姓名");
        writer.addHeaderAlias("staffHeadImage", "照片");
        writer.addHeaderAlias("staffUnit", "单位名称");
        writer.addHeaderAlias("staffTel", "手机号");
        writer.addHeaderAlias("staffCard", "身份证类别");
        writer.addHeaderAlias("staffCardid", "身份证件号码");
        writer.addHeaderAlias("projectType", "所属项目");
        writer.addHeaderAlias("projectLabel", "标签");
        writer.addHeaderAlias("projectRole", "项目角色");
        writer.addHeaderAlias("icCard", "IC卡号");
        writer.addHeaderAlias("qrcode", "二维码号");
        writer.addHeaderAlias("intoAccount", "录入账号或来源");
        writer.addHeaderAlias("intoTime", "录入时间");
        writer.addHeaderAlias("staffState", "人员状态");

        writer.setColumnWidth(1, 13);
        writer.setColumnWidth(3, 13);
        writer.setColumnWidth(4, 13);
        writer.setColumnWidth(5, 16);
        writer.setColumnWidth(6, 15);
        writer.merge(13, "人员管理", true);
    }

    /**
     * 获取最大最小id
     *
     * @param name
     * @return
     */
    @PostMapping(value = "/selectId")
    public Map<String, Object> selectId(String name) {
        Map<String, Object> map = new HashMap<String, Object>();
        String code = "500";
        String msg = "失败";
        StaffExample example = new StaffExample();
        StaffExample.Criteria criteria = example.createCriteria();
        criteria.andProjectTypeEqualTo(name);
        int maxId = -1;
        int minId = -1;

        maxId = staffBiz.selectMaxId(example);
        minId = staffBiz.selectMinId(example);
        if (-1 != maxId && -1 != minId) {
            code = "200";
            msg = "成功";
        }
        map.put("maxId", maxId);
        map.put("minId", minId);
        map.put("code", code);
        map.put("msg", msg);
        return map;
    }

    /**
     * 获取人员数据
     *
     * @param minId
     * @param maxId
     * @param name  项目名
     * @return
     */
    @PostMapping(value = "/selectByIdAll")
    public Map<String, Object> selectByIdAll(int minId, int maxId, String name) {
        Map<String, Object> map = new HashMap<String, Object>();
        String code = "500";
        String msg = "失败";
        StaffExample example = new StaffExample();
        StaffExample.Criteria criteria = example.createCriteria();
//        criteria.andProjectTypeEqualTo(name);
//        criteria.andStaffIdGreaterThanOrEqualTo(minId);
//        criteria.andStaffIdLessThanOrEqualTo(maxId);
        List<Staff> staffList = staffBiz.selectByExample(example);
        if (null != staffList && staffList.size() > 0) {
            code = "200";
            msg = "成功";
        }
        map.put("data", staffList);
        map.put("code", code);
        map.put("msg", msg);
        return map;
    }


    /**
     * test 导出数据
     *
     * @param name 项目名
     * @return
     */
    @PostMapping(value = "/test")
    public void test(@RequestParam("name") String name, HttpServletResponse response) throws IOException {
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> mapA = new HashMap<String, Object>();
        int maxId = -1;
        int minId = -1;
        map = selectId(name);
        if ("200".equals(map.get("code"))) {
            maxId = (int) map.get("maxId");
            minId = (int) map.get("minId");
            mapA = selectByIdAll(minId, maxId, name);
            System.out.println(mapA);
        }

        PageRequest pageRequest = new PageRequest();
        PageResult result = null;


        List<Staff> content = (List<Staff>) mapA.get("data");
        long startTime = System.currentTimeMillis();   //获取开始时间
        List<StaffVo> collect = content.stream().map((e) -> {
            StaffVo staffVo = new StaffVo();
            BeanUtils.copyProperties(e, staffVo);
            return staffVo;
        }).collect(Collectors.toList());

        List<StaffVo> rows1 = null;
        for (int i = 0; i < collect.size(); i++) {
            rows1 = CollUtil.newArrayList(collect);
        }

        BigExcelWriter writer = ExcelUtil.getBigWriter("e:/xxx890.xlsx");
        getStyle(writer);
//      一次性写出内容，使用默认样式
        writer.write(rows1);
        long endTime = System.currentTimeMillis(); //获取结束时间
        long l = endTime - startTime;
        System.out.println("导出共耗时>>>>>>>>" + l + "毫秒");
//        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
//        ServletOutputStream out = response.getOutputStream();
//        writer.flush(out, true);
        // 关闭writer，释放内存
        writer.close();
//        IoUtil.close(out);

    }


    /**
     * @return
     */
    @PostMapping(value = "/test2")
    public void test2(HttpServletResponse response) throws IOException {
        Map<String, Object> map = new HashMap<String, Object>();
        long startTime = System.currentTimeMillis();   //获取开始时间
        List<?> rows1 = null;
        for (int i = 1; i < 20000; i++) {
            List<?> row1 = CollUtil.newArrayList("aa", "bb", "cc", "dd", DateUtil.date(), 3.22676575765);
            rows1 = CollUtil.newArrayList(row1);
        }

//        ExcelWriter writer = ExcelUtil.getWriter("e:/writeTest.xlsx");
//
//        // 一次性写出内容，使用默认样式，强制输出标题
//        writer.write(rows1, true);
//        long endTime = System.currentTimeMillis(); //获取结束时间
//        long l = endTime - startTime;
//        System.out.println("导出共耗时>>>>>>>>" + l + "毫秒");
//        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
//        response.setHeader("Content-Disposition", "attachment;filename=test.xlsx");
//        ServletOutputStream out = response.getOutputStream();
//        writer.flush(out, true);
//        writer.close();
//        IoUtil.close(out);

        BigExcelWriter writer = ExcelUtil.getBigWriter("e:/xx1x1.xlsx");
//// 一次性写出内容，使用默认样式
        writer.write(rows1);
        long endTime = System.currentTimeMillis(); //获取结束时间
        long l = endTime - startTime;
        System.out.println("导出共耗时>>>>>>>>" + l + "毫秒");
//// 关闭writer，释放内存
        writer.close();

    }

}
